package com.qf.android.sport.fragment.headline;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.util.Pair;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.qf.android.sport.R;
import com.qf.android.sport.activity.MainActivity;
import com.qf.android.sport.adapter.headline.HeadlinePagerAdapter;
import com.qf.android.sport.databinding.FragmentHeadlineBinding;
import com.qf.android.sport.entity.headline.Dynamic;
import com.qf.android.sport.entity.headline.Head;
import com.qf.android.sport.utils.headline.HeadlineHttpService;

import java.util.ArrayList;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


/**
 * A simple {@link Fragment} subclass.
 */
public class HeadlineFragment extends Fragment implements Callback<Head> {


    private FragmentHeadlineBinding binding;
    private HeadlineHttpService service;
    private HeadlinePagerAdapter adapter;
    private Dynamic.DataBean dynamicDataBean;
    private ViewPager headlineViewPager;
    public HeadlineFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Fresco.initialize(getContext());
        service = new Retrofit.Builder()
                .baseUrl("http://www.baidu.com")
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(HeadlineHttpService.class);
        service.getHead().enqueue(this);
        service.getDynamic().enqueue(new Callback<Dynamic>() {
            @Override
            public void onResponse(Call<Dynamic> call, Response<Dynamic> response) {
                dynamicDataBean = response.body().getData();
                if (adapter!= null){
                    adapter.add(1,response.body().getData());
                }

            }

            @Override
            public void onFailure(Call<Dynamic> call, Throwable t) {

            }
        });
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        binding = FragmentHeadlineBinding.inflate(LayoutInflater.from(getContext()), container, false);
        ArrayList<Pair<Integer,Integer>> list = new ArrayList<>();
        list.add(new Pair<Integer, Integer>(R.layout.layout_headline_headlineroot, com.qf.android.sport.BR.headlineDataBean));
        list.add(new Pair<Integer, Integer>(R.layout.layout_headline_dynamicroot, com.qf.android.sport.BR.dynamicDataBean));
        adapter = new HeadlinePagerAdapter(list);
        binding.headlineRootVp.setAdapter(adapter);
        headlineViewPager = binding.headlineRootVp;
        headlineViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

                MainActivity activity = (MainActivity) getActivity();
                if(position == 0){
                    activity.getTopRadioGroup().setSelected(false);
                    activity.getTopRadioGroup().check(R.id.rtn1);
                }else if (position == 1){
                    activity.getTopRadioGroup().setSelected(true);
                    activity.getTopRadioGroup().check(R.id.rtn2);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        return binding.getRoot();

    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
       // HeadlineHeadlineRecyclerAdapter adapter = new HeadlineHeadlineRecyclerAdapter(getContext(),list);
       // recyclerView.setAdapter(adapter);

        if (dynamicDataBean != null){
            adapter.add(1,dynamicDataBean);
        }

    }

    @Override
    public void onResponse(Call<Head> call, Response<Head> response) {
       adapter.add(0,response.body().getData());
    }

    @Override
    public void onFailure(Call<Head> call, Throwable t) {
        Toast.makeText(getContext(),"数据下载失败",Toast.LENGTH_SHORT).show();
    }


    public ViewPager getHeadlineViewPager() {
        return headlineViewPager;
    }
}
